package com.hn.gege.im.modules.im.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.im.modules.im.entity.ChatMessageUnread;
import com.hn.gege.im.modules.im.mapper.ChatMessageUnreadMapper;
import com.hn.gege.im.modules.im.service.IChatMessageUnreadService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 未读聊天消息 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2021-10-12
 */
@Service
public class ChatMessageUnreadServiceImpl extends ServiceImpl<ChatMessageUnreadMapper, ChatMessageUnread> implements IChatMessageUnreadService {


    @Override
    public boolean add(ChatMessageUnread messageUnread) {
        return super.save(messageUnread);
    }

    @Override
    public boolean remove(String sender, String receiver) {
        LambdaQueryWrapper<ChatMessageUnread> eq = new QueryWrapper<ChatMessageUnread>().lambda()
                .eq(ChatMessageUnread::getSender, sender)
                .eq(ChatMessageUnread::getReceiver, receiver);
        return super.remove(eq);
    }

    @Override
    public List<ChatMessageUnread> listBySenderAndReceiver(String sender, String receiver) {
        LambdaQueryWrapper<ChatMessageUnread> eq = new QueryWrapper<ChatMessageUnread>().lambda()
                .eq(ChatMessageUnread::getSender, sender)
                .eq(ChatMessageUnread::getReceiver, receiver)
                .orderByDesc(ChatMessageUnread::getTimestamp);
        List<ChatMessageUnread> list = super.list(eq);
        Collections.reverse(list);
        return list;
    }

    @Override
    public Page<ChatMessageUnread> listPageBySenderAndReceiver(String msgId, Integer limit, String sender, String receiver) {
        Page<ChatMessageUnread> pageInfo = new Page<>(1, limit);
        LambdaQueryWrapper<ChatMessageUnread> eq = new QueryWrapper<ChatMessageUnread>().lambda()
                .eq(ChatMessageUnread::getSender, sender)
                .eq(ChatMessageUnread::getReceiver, receiver)
                .lt(StrUtil.isNotBlank(msgId), ChatMessageUnread::getMsgId, msgId)
                .orderByDesc(ChatMessageUnread::getTimestamp);
        Page<ChatMessageUnread> page = super.page(pageInfo, eq);
        List<ChatMessageUnread> records = page.getRecords();
        Collections.reverse(records);
        return page;
    }

    @Override
    public Integer count(String sender, String receiver) {
        LambdaQueryWrapper<ChatMessageUnread> eq = new QueryWrapper<ChatMessageUnread>().lambda()
                .eq(ChatMessageUnread::getSender, sender)
                .eq(ChatMessageUnread::getReceiver, receiver)
                .orderByDesc(ChatMessageUnread::getTimestamp);
        return super.count(eq);
    }
}
